//
//  OrderListViewController.m
//  XDCommonApp
//
//  Created by xindao on 14-7-1.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "OrderListViewController.h"
#import "OrderTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "OrderDetailViewController.h"
@interface OrderListViewController ()

@end

@implementation OrderListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.titleLabel.text = @"我的订单";

    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, UI_MAINSCREEN_HEIGHT - UI_NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:myTableView];

    [self getData];
}


- (void)getData{
    if ([XDTools NetworkReachable]){
        NSString * phoneNum = [[NSUserDefaults standardUserDefaults] objectForKey:kMMyUserInfo][0];
        NSDictionary * dic = @{@"phone":phoneNum};

        NSMutableDictionary *muDict = [[NSMutableDictionary alloc] initWithDictionary:dic];
        NSMutableString *params = [[NSMutableString alloc] init];
        for (id keys in muDict) {
            [params appendFormat:@"&%@=%@",keys,[muDict objectForKey:keys]];
        }
        //        NSRange rang = [params rangeOfString:@"?"];
        //        if(rang.location == NSNotFound){
        //            NSRange rangAnd = [params rangeOfString:@"&"];
        //            [params replaceCharactersInRange:rangAnd withString:@"?"];
        //        }

        NSString *urlStr = [NSString stringWithFormat:@"http://www.xindaoapp.com/maodai/plugins/API.v1.0/?&a=category&m=order_list%@",params];
        urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        DDLOG(@"urlStr:%@", urlStr);
        NSURL *url = [NSURL URLWithString:urlStr];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setTimeOutSeconds:12];

        ASIHTTPRequest * mrequest = request;

        [request setCompletionBlock:^{
            [XDTools hideProgress:self.contentView];
            NSString *responseString = [mrequest responseString];
            DDLOG(@"zhifu responseString:%@", responseString);

            NSDictionary * tempDic = [XDTools JSonFromString:[mrequest responseString]];

            if ([tempDic[@"result"] intValue]) {
                dataArray = [NSMutableArray arrayWithArray:tempDic[@"data"]];
                [myTableView reloadData];
            }else{
                [XDTools showTips:tempDic[@"msg"] toView:self.view];
            }

        }];

        [request setFailedBlock:^{
            [XDTools hideProgress:self.contentView];
            NSLog(@"%@",mrequest.error.description);
        }];
        [request startAsynchronous];
        [XDTools showProgress:self.contentView];
    }else{
        [XDTools showTips:brokenNetwork toView:self.contentView];
    }
    
}



#pragma mark - TableView DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellIde = @"cell";
    OrderTableViewCell * cell = [myTableView dequeueReusableCellWithIdentifier:cellIde];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.lineIV.frame = CGRectMake(0, 114.5f, 320, .5f);
    }
    [cell.headIV setImageWithURL:[NSURL URLWithString:dataArray[indexPath.row][@"goods_img"]] placeholderImage:nil];
    cell.nameLB.text = dataArray[indexPath.row][@"goods_name"];
    cell.timeLB.text = [NSString stringWithFormat:@"订单时间:%@",dataArray[indexPath.row][@"addtime"]];
    cell.priceLB.text = [NSString stringWithFormat:@"￥%.2f",[dataArray[indexPath.row][@"shop_price"] floatValue]/100];
    cell.priceLB.textColor = RGBA(245, 170, 87, 1);
    return cell;
}

#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderDetailViewController * detail = [[OrderDetailViewController alloc] init];
    detail.addTime = dataArray[indexPath.row][@"addtime"];
    detail.timeStr = [NSString stringWithFormat:@"%@",dataArray[indexPath.row][@"unix"]];
    detail.shop_price = dataArray[indexPath.row][@"shop_price"];
    detail.goods_img = dataArray[indexPath.row][@"goods_img"];
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
